package com.item.mobile;

import static com.item.mobile.MobileUtil.checkParam;
import static com.base.util.StringUtil.emptyIfBlank;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.base.dialect.PaginationSupport;
import com.base.mobile.MobileInfo;
import com.base.mobile.server.annotation.Mobile;
import com.base.mobile.server.annotation.MobileMethod;
import com.base.util.DateUtil;
import com.item.dao.model.Diary;
import com.item.dao.model.DiaryExample;
import com.base.util.StringUtil;
import com.item.dao.model.Question;
import com.item.dao.model.QuestionExample;
import com.item.dao.model.QuestionRecommend;
import com.item.dao.model.QuestionRecommendExample;
import com.item.dao.model.Sign;
import com.item.dao.model.SignExample;
import com.item.dao.model.User;
import com.item.daoEx.model.StatusCountEx;
import com.item.service.DiaryService;
import com.item.service.QuestionRecommendService;
import com.item.service.QuestionService;
import com.item.service.SignService;
import com.item.service.StatusCountService;
import com.item.service.UserService;
import com.item.util.ConstantResources;
import com.item.util.MyDateUtils;
import com.item.util.SubjectUtils;
import com.mobile.api.proto.MAppIndex.MDiary;
import com.mobile.api.proto.MAppIndex.MDiaryList;
import com.mobile.api.proto.MAppQuestion.MQuestion;
import com.mobile.api.proto.MAppQuestion.MQuestionCount;
import com.mobile.api.proto.MAppQuestion.MQuestions;
import com.mobile.api.proto.MSystem.MRet;

@Component
@Mobile
public class MAppDiary {

	@Autowired
	private DiaryService diaryService;
	@Autowired
	private SignService signService;
	
	@Autowired
	private StatusCountService statusService;
	
	
	
	@MobileMethod(methodno = "MUploadDiary", isLogin = true)
	public MRet.Builder uploadDiary(Diary diary)throws Exception{
		//检查参数
		checkParam(diary.getContent(), "content");
		//日期不传默认为今天
		if(diary.getDate() == null){
			diary.setDate(MyDateUtils.parseDateWithDay(new Date()));
		}
		updateDiary(diary);
		MRet.Builder ret = MRet.newBuilder();
		ret.setCode(1);
		ret.setMsg("同步成功");
		return ret;
	}
	
	public void updateDiary(Diary diary){
		DiaryExample dex = new DiaryExample();
		dex.createCriteria().andUseridEqualTo(diary.getUserid()).andDateEqualTo(diary.getDate());
		List<Diary> lists = diaryService.selectByExample(dex);
		//检查今天是否有日记了
		if(lists == null || lists.size() < 1){
			diary.setCreateTime(DateUtil.parseDate(diary.getDate(), "yyyy-MM-dd"));
			diaryService.insert(diary);
		}else{
			Diary d = lists.get(0);
			d.setModifyTime(new Date());
			d.setContent(diary.getContent());
			diaryService.updateByPrimaryKeySelective(d);
		}
	}
	
	@MobileMethod(methodno = "MGetDiaries", isLogin = true)
	public MDiaryList.Builder getDiaries(MobileInfo mobileInfo,String date, Integer days) throws Exception{
		Date now = new Date();
		if(StringUtils.isBlank(date)){
			date = MyDateUtils.parseDateWithDay(now);
		}
		if(days == null){
			days = 10;
		}
		//默认取从今天开始以后的10天
		DiaryExample dex = new DiaryExample();
		dex.setOrderByClause("date ASC");
		dex.createCriteria().andUseridEqualTo(mobileInfo.getUserid()).andDateBetween(date, MyDateUtils.dateStringAdd(date, days));
		List<Diary> lists = diaryService.selectByExample(dex);
		MDiaryList.Builder ret = assembleData(lists);
		return ret;
	}
	
	@MobileMethod(methodno = "MSign", isLogin = true)
	public MRet.Builder sign(MobileInfo mobileInfo,String subject, Integer type,Date date,String diaryContent)throws Exception{
		//检查参数
//		checkParam(sign.getType(), "type");
		checkParam(date, "date");
		//checkParam(sign.getSubject(),"subject");
		//
		Sign sign = new Sign();
		sign.setUserid(mobileInfo.getUserid());
		sign.setDate(date);
		if(StringUtils.isBlank(subject)){
			sign.setSubject("打卡");
		}
		if(type == null){
			Integer typeTemp = SubjectUtils.getTyepBySubject(sign.getSubject());
			sign.setType(typeTemp == null ? 0 : typeTemp);
		}
			
		SignExample sex = new SignExample();
		sex.createCriteria().andUseridEqualTo(mobileInfo.getUserid()).andDateEqualTo(date);
		List<Sign> signs = signService.selectByExample(sex);
		if(signs == null || signs.size() < 1){
			sign.setCount(1);
			sign.setCreateTime(date);
			signService.insertSelective(sign);
		}
		//如果有日记的内容，那么也同步下日记
		if(StringUtils.isNotBlank(diaryContent)){
			Diary diary = new Diary();
			diary.setContent(diaryContent);
			diary.setDate(MyDateUtils.parseDateWithDay(date));
			diary.setUserid(mobileInfo.getUserid());
			updateDiary(diary);
		}
		MRet.Builder ret = MRet.newBuilder();
		ret.setCode(1);
		ret.setMsg(String.format("%s?userid=%s&t=%d", ConstantResources.SIGNSHAREURL,sign.getUserid(),System.currentTimeMillis()));
		return ret;
	}
	
	@MobileMethod(methodno = "MSignCountStatus", isLogin = true) 
	public MQuestionCount.Builder signCount(MobileInfo mobileInfo,String date) throws Exception {
		if(StringUtils.isBlank(date)){
			date = MyDateUtils.parseDateWithDay(new Date());
		}
		Map<String,String> map = new HashMap<String,String>();
		map.put("userid", mobileInfo.getUserid());
		map.put("date", date);
		StatusCountEx status = statusService.getSignCount(map);
		MQuestionCount.Builder ret = assembleData(status);
		return ret;
	}
	
	public static MQuestionCount.Builder assembleData(StatusCountEx status){
		MQuestionCount.Builder ret = MQuestionCount.newBuilder();
		ret.setTotoalCount(status.getTotalCount()); 
		ret.setEngCount(status.getEngCount());
		ret.setMathCount(status.getMathCount());
		ret.setMajor1Count(status.getMajor1Count());
		ret.setMajor2Count(status.getMajor2Count());
		return ret;
	}
	
	public static MDiaryList.Builder assembleData(List<Diary> lists){
		MDiaryList.Builder ret = MDiaryList.newBuilder();
		for(Diary diary : lists){
			MDiary.Builder d = MDiary.newBuilder();
			d.setContent(diary.getContent());
			d.setDate(diary.getDate());
			d.setId(diary.getId());
			d.setUserid(diary.getUserid());
			ret.addDiary(d);
		}
		return ret;
	}

	
}
